//
//  mapView.m
//  map
//
//  Created by 王彦森 on 16/4/25.
//  Copyright © 2016年 王彦森. All rights reserved.
//

#import "mapView.h"




@interface mapView () <MAMapViewDelegate,AMapSearchDelegate,UITableViewDataSource,UITableViewDelegate,CLLocationManagerDelegate,AMapNaviManagerDelegate>

@end

@implementation mapView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [MAMapServices sharedServices].apiKey =@"08311feb492c0460ac506596b7fadbc1";
        _mapView = [[MAMapView alloc]initWithFrame:self.frame];
        self.mapView.delegate= self;
        [self addSubview:_mapView];
        
        self.Locaton = [UIButton buttonWithType:UIButtonTypeSystem];
        self.Locaton.frame = CGRectMake(10, self.frame.size.height-150, 35, 35);
        self.Locaton.backgroundColor =[UIColor whiteColor];
        [self.Locaton setImage:[UIImage imageNamed:@"定位"] forState:UIControlStateNormal];
        [self.Locaton addTarget:self action:@selector(Locaton:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.Locaton];
        
        self.searchButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.searchButton setTintColor:[UIColor blackColor]];
        self.searchButton.frame = CGRectMake(5, self.frame.size.height-95, 80, 30);
        self.searchButton.backgroundColor =[UIColor whiteColor];
        [self.searchButton setTitle:@"导航" forState:UIControlStateNormal];
        [self.searchButton addTarget:self action:@selector(pathAction) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.searchButton];
        
//        self.lable=[[UILabel alloc] initWithFrame:CGRectMake(90, self.frame.size.height-35, 80, 30)];
//        self.lable.center = CGPointMake(self.center.x, self.frame.size.height-35/2);
//        self.lable.backgroundColor = [UIColor cyanColor];
//        [self addSubview:self.lable];
        
        self.searchField=[[UITextField alloc]initWithFrame:CGRectMake(10, 125, 100, 30)];
        self.searchField.backgroundColor = [UIColor lightGrayColor];
        _searchField.alpha = 0.5;
        [self addSubview:self.searchField];
        
        UIButton*sear = [UIButton buttonWithType:UIButtonTypeSystem];
        sear.frame = CGRectMake(10, 160, 50, 30);
        sear.backgroundColor = [UIColor whiteColor];
        [sear setTintColor:[UIColor blackColor]];
        [sear setTitle:@"查询" forState:UIControlStateNormal];
        [sear addTarget:self action:@selector(searchField:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:sear];
        
        self.textField = [[UITextField alloc]initWithFrame:CGRectMake(10, 90, 100, 30)];
        self.textField.backgroundColor = [UIColor lightGrayColor];
        self.textField.alpha = 0.5;
        [self addSubview:self.textField];
    }
    return self;
}
#pragma mark 开始导航
-(void)pathAction{
    if (_search == nil) {
        return;
    }
    AMapRouteShareSearchRequest *request = [[AMapRouteShareSearchRequest alloc] init];
    request.strategy = 2;
    request.type = 2;
    request.startCoordinate.latitude = self.latitude;
    request.startCoordinate.longitude = self.longitude;
    
    request.destinationCoordinate.latitude = self.la;
    request.destinationCoordinate.longitude = self.lo;
    
    
    [_search AMapRouteShareSearch:request];
    
}



//实现POI搜索对应的回调函数
- (void)onPOISearchDone:(AMapPOISearchBaseRequest *)request response:(AMapPOISearchResponse *)response
{
    if(response.pois.count == 0)
    {
        return;
    }
    //通过 AMapPOISearchResponse 对象处理搜索结果
    NSString *strCount = [NSString stringWithFormat:@"count: %ld",response.count];
    NSString *strSuggestion = [NSString stringWithFormat:@"Suggestion: %@", response.suggestion];
    NSString *strPoi = @"";
    for (AMapPOI *p in response.pois) {
        strPoi = [NSString stringWithFormat:@"%@\nPOI: %@", strPoi, p.description];
    }
    NSString *result = [NSString stringWithFormat:@"%@ \n %@ \n %@", strCount, strSuggestion, strPoi];
    NSLog(@"Place: %@", result);
}


#pragma mark 开始定位模式
-(void)Locaton:(UIButton*)but{
    self.mapView.showsUserLocation = YES;
    //用户跟踪模式
    [self.mapView setUserTrackingMode:MAUserTrackingModeFollowWithHeading animated:YES];
}
#pragma mark 正向地理的
-(void)searchField:(UIButton*)text{
    self.TipsSearch = [[UITableView alloc]initWithFrame:CGRectMake(0, self.frame.size.height/2, self.frame.size.width, self.frame.size.height/2)];
    self.TipsSearch.delegate = self;
    self.TipsSearch.dataSource =self;
    [self.TipsSearch registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];
    [self addSubview:self.TipsSearch];
    
    [AMapSearchServices sharedServices].apiKey = @"08311feb492c0460ac506596b7fadbc1";
    self.search = [[AMapSearchAPI alloc]init];
    self.search.delegate = self;
    //构造AMapInputTipsSearchRequest对象,设置请求参数
    AMapInputTipsSearchRequest *request = [[AMapInputTipsSearchRequest alloc]init];
    request.keywords = self.searchField.text;
    request.city = self.textField.text;
    NSLog(@" request.city=========%@",request.city);
    //发起输入提示搜索
    
    [self animal];
    [self.search AMapInputTipsSearch:request];
}
-(void)animal{
    [UIView animateWithDuration:0.5 animations:^{
        _mapView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height/2);
        self.Locaton.frame = CGRectMake(10, self.frame.size.height/2 - 55, 35, 35);
    } completion:^(BOOL finished) {
        
    }];
}

#pragma mark 当位置更新是,会进定位回调,通过回调函数,能获取到定位点的经纬度
-(void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation{
    if (updatingLocation) {
        //取出当前位置的坐标
        NSLog(@"latitude : %f,longitude: %f",userLocation.coordinate.latitude,userLocation.coordinate.longitude);
        self.latitude =userLocation.coordinate.latitude;
        self.longitude =userLocation.coordinate.longitude;
//        [self.searchButton addTarget:self action:@selector(search:) forControlEvents:UIControlEventTouchUpInside];
        
        
    }
    
}
//实现逆地理编码的回调函数
-(void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response{
    if (response.regeocode !=nil) {
        //通过AMapReGeocodeSearchResponse对象处理搜索结果
        NSString *result = [NSString stringWithFormat:@" %@", response.regeocode.addressComponent.city];
        NSLog(@"=======city:%@",result);
        self.rowCity = result;
        self.lable.text =result;
        NSLog(@"%@",self.lable.text);
    }
    
}
//实现输入提示的回调函数
-(void)onInputTipsSearchDone:(AMapInputTipsSearchRequest *)request response:(AMapInputTipsSearchResponse *)response{
    if (response.tips.count == 0) {
        return;
    }
    //通过AMapInputTipsSearchResponse对象处理搜索结果
    NSString *strcount = [NSString stringWithFormat:@"count :%ld",response.count];
    NSString *strTips= @"";
    self.dataArr = [NSMutableArray array];
    for (AMapTip *p in response.tips) {
        [self.dataArr addObject:p];
        [self.TipsSearch reloadData];
        strTips = [NSString stringWithFormat:@"%@\nTip:%@",strTips,p.description];
        NSLog(@"%@",p.name);
    }
    NSString *Result = [NSString stringWithFormat:@"%@\n%@",strcount,strTips];
    NSLog(@"InputTips: %@", Result);
}


-(void)hinde:(UIButton*)but{
    but.hidden = YES;
    self.TipsSearch.hidden = YES;
    
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.searchField resignFirstResponder];
    [self.textField resignFirstResponder];
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
    
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    model *d = [[model alloc]init];
    d = self.dataArr[indexPath.row];
    cell.textLabel.text = d.name;
    return cell;
}
#pragma mark tableView 的点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //  在地图上添加大头针   根据点击的Cell
    AMapPOI *poi = _dataArr[indexPath.row];
    MAPointAnnotation *annotation = [[MAPointAnnotation alloc] init];
    annotation.coordinate = CLLocationCoordinate2DMake(poi.location.latitude,poi.location.longitude);
    self.la = poi.location.latitude;
    self.lo = poi.location.longitude;
    annotation.title = poi.name;
//    annotation.subtitle = poi.address;
    _mapView.centerCoordinate = CLLocationCoordinate2DMake(poi.location.latitude,poi.location.longitude);
    [_annoArr addObject:annotation];
    [self.mapView addAnnotation:annotation];
}
#pragma mark 搜索的回调方法
-(MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation{
    if ([annotation isKindOfClass:[MAPointAnnotation class]]) {
//        MAAnnotationView 的从用机制
        static NSString *reuseIndetifier = @"annotationReuseIndetifier";
        MAPinAnnotationView *annotationView = (MAPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIndetifier];
        if (annotationView == nil) {
            annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIndetifier];
        }
        annotationView.canShowCallout = YES;
        return annotationView;
    }
    return nil;
}

@end
